package com.example.cardgame.utils

import android.content.Context
import com.example.cardgame.model.UserInfo
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

object FileUtils {
    fun loadUserInfo(context: Context, fileName: String): UserInfo? {
        return try {
            val fileInputStream = context.openFileInput(fileName)
            val objectInputStream = ObjectInputStream(fileInputStream)
            val userInfo = objectInputStream.readObject() as UserInfo
            objectInputStream.close()
            fileInputStream.close()
            userInfo
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

    fun saveUserInfo(context: Context, fileName: String, userInfo: UserInfo) {
        try {
            val fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE)
            val objectOutputStream = ObjectOutputStream(fileOutputStream)
            objectOutputStream.writeObject(userInfo)
            objectOutputStream.close()
            fileOutputStream.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}